package com.zhitan.ems.app.stat.config.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhitan.ems.app.stat.config.entity.ElectricityTypeSettingEntity;
import com.zhitan.ems.app.stat.config.entity.ElectricityTypeSettingItemEntity;
import com.zhitan.ems.app.stat.config.mapper.ElectricityTypeSettingMapper;
import com.zhitan.ems.app.stat.config.service.ElectricityTypeSettingService;
import com.zhitan.ems.app.stat.config.util.ElectricityCalculationUtil;
import com.zhitan.ems.framework.core.mp.base.BaseServiceImpl;
import com.zhitan.ems.framework.core.vo.VQueryKit;

@Service
public class ElectricityTypeSettingServiceImpl
		extends BaseServiceImpl<ElectricityTypeSettingMapper, ElectricityTypeSettingEntity>
		implements ElectricityTypeSettingService {

	/**
	 * 根据锋平谷配置计算24小时电费
	 * 
	 * @param hourlyConsumption 24小时用电量数组 (kWh)，索引0-23对应0点到23点
	 * @param settings          锋平谷配置项列表
	 * @return 包含总电费和分类电费的结果
	 */
	public Map<String, Object> calculate24HourElectricityCost(BigDecimal[] hourlyConsumption,
			List<ElectricityTypeSettingItemEntity> settings) {
		return ElectricityCalculationUtil.calculate24HourElectricityCost(hourlyConsumption, settings);
	}

	/**
	 * 根据锋平谷配置和单一电价计算24小时电费
	 * 
	 * @param hourlyConsumption  24小时用电量数组 (kWh)，索引0-23对应0点到23点
	 * @param settings           锋平谷配置项列表
	 * @param singlePrice        单一电价（如果不使用锋平谷计费，则使用此电价）
	 * @param usePeakValleyPrice 是否使用锋平谷电价，如果为false则使用单一电价
	 * @return 包含总电费和分类电费的结果
	 */
	public Map<String, Object> calculate24HourElectricityCost(BigDecimal[] hourlyConsumption,
			List<ElectricityTypeSettingItemEntity> settings, BigDecimal singlePrice, boolean usePeakValleyPrice) {
		return ElectricityCalculationUtil.calculate24HourElectricityCost(hourlyConsumption, settings, singlePrice,
				usePeakValleyPrice);
	}
	@Override
	public ElectricityTypeSettingEntity getBase() {
		QueryWrapper<ElectricityTypeSettingEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(VQueryKit.getUnderColumn("isBase"), 1);
		queryWrapper.ge("start_time", new Date());
		queryWrapper.orderByDesc("start_time");
		List<ElectricityTypeSettingEntity> list=this.list(queryWrapper);
		return list.size()>0?list.get(0):null;
	}
}